<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/18 0018
 * Time: 下午 1:48
 * 王晔
 * 商家控制器
 */
namespace app\admin\controller;
use app\admin\Model\Classify;
use app\common\controller\Curt;
use think\Db;
use think\Validate;
use think\Request;
class  Shop extends  Curt {
    function __construct()
    {
        parent::__construct($upload = true,$uploadConfig = array(
            'input_name' => 'coverFile',//文本框name
            'data_field' => 'shop_logo'//数据库字段
        ));
    }
    public function getall()
    {
        $data=$this->request->param();
        $where['status']=1;
        if( trim($data['keyword'])){
            $where['shop_username|shop_name|shop_person|shop_tel']=['like','%'.trim($data['keyword']).'%'];//模糊查询or可以一个条件中写多个字段以‘|’分开
        }

        $list = $this->model->where($where)->order('id desc')->paginate();
        foreach ($list as $k=>$v){
            if($data['classify_id']){
                $arr=explode(',',$v['classify_id']);
                 if(!in_array($data['classify_id'],$arr)){
                     unset($list[$k]);
                 }
            }
            $list[$k]['classify_names']=$this->getclassifylist($v['classify_id']);
        }

        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
    }
    /**
     * @param $ids
     * @return string
     */
    protected function getclassifylist($ids){
        $names="";
        $list=Db::name('classify')->where('id','in',$ids)->select();
        foreach ($list as $v){
            $names.=" &nbsp;".$v['classify_name']."&nbsp;|";
        }
        $names= trim($names,"|");
        return $names;
    }
    /**
     * 添加商户
     */
    public function adds(){
        $data=$this->request->param();
        $rule = [
            'shop_username'=>['require','unique:shop','max:10','min:4'],//登录名
        ];
        $msg = [
            'shop_username.require'   => '名称必须填写！',
            'shop_username.min'   => '名称最短二位!',
            'shop_username.max'   => '名称最长十位!',
            'shop_username.unique'   => '名称已存在！',
        ];
        $validate   = Validate::make($rule,$msg);
        $result = $validate->check($data);
        if(!$result) {
            $this->error($validate->getError());
        }
        $data['shop_pwd']=md5($data['shop_pwd']);
        $data['shop_status']=1;
        $res=$this->model->allowField(true)->save($data);
        if ($res) {
            $this->success('商户添加成功！', $this->fetchconfig['add_success']);
        } else {
            $this->success('数据异常！', $this->fetchconfig['add_success']);
        }
    }
    /**
     * 密码重置
     */
    public function refresspwd(){
        $data['id']=input('id');
        $data['shop_pwd']=md5('a123456');
        $this->model->isUpdate(true)->save($data);
        $datas['state']=1;
        $datas['msg']='密码重置成功';
        return  json($datas);
    }
    /**
     * @return \think\response\Json
     * 开启关闭状态
     */
    public function editstatus(){
        $where['id']=input('id');
        if(input('type')==1){
            $status=input('shop_top');
            if($status==1){
                $msg="取消置顶成功!";
                $status=2;
            }else{
                $msg="置顶成功!";
                $status=1;
                $where['shop_toptime']=time();
            }
            $where['shop_top']=$status;
        }else{
            $status=input('shop_status');
            if($status==1){
                $msg="拉黑成功!";
                $status=2;
            }else{
                $msg="取消拉黑成功!";
                $status=1;
            }
            $where['shop_status']=$status;
        }
        $this->model->isUpdate(true)->save($where);
        $res['msg']=$msg;
        return json($res);
    }
    /**
     * 推荐商户
     */
    public function isshow()
    {
        $status = Request::instance()->param('is_show');
        $where['id'] = Request::instance()->param('id');
        $where['is_show'] = $status == 1 ? 2 : 1;
        if ($where['id']) {
            $res = $this->model->isUpdate(true)->save($where);
            if ($res) {
                $this->success('操作成功', $this->fetchconfig['add_success']);
            } else {
                $this->error('网络繁忙，请稍后重试', $this->fetchconfig['add_success']);
            }
        }
    }
}